#include "stdafx.h"
#include "JGraph.h"
#include <algorithm>
#include "JBoundary.h"
#include "JPiece.h"
#include "JPoint.h"

JGraph::JGraph()
{
}


JGraph::~JGraph()
{
}

int JGraph::GetBoundaryCount()
{
	return (int)m_pBoundaries.size();
}

JBoundary* JGraph::GetBoundary(int _idx)
{
	return m_pBoundaries[_idx];
}

JBoundary* JGraph::GetBoundary(int _ptIdx1, int _ptIdx2)
{
	JPoint* pP1 = m_pPoints[_ptIdx1];
	JPoint* pP2 = m_pPoints[_ptIdx2];
	vector<JBoundary*>::iterator it = find_if(m_pBoundaries.begin(), m_pBoundaries.end(),
		[&](JBoundary* _pBoundary)->bool{
		return _pBoundary->IsSameBoundary(pP1, pP2);
	});
	if (it != m_pBoundaries.end())
	{
		return *it;
	}
	return NULL;
}

int JGraph::GetPieceCount()
{
	return (int)m_pPieces.size();
}

JPiece* JGraph::GetPiece(int _idx)
{
	return m_pPieces[_idx];
}

int JGraph::GetPointCount()
{
	return (int)m_pPoints.size();
}

JPoint* JGraph::GetPoint(int _idx)
{
	return m_pPoints[_idx];
}

void JGraph::AddPoint(JPoint* _pPoint)
{
	m_pPoints.push_back(_pPoint);
}

void JGraph::AddBoundary(JBoundary* _pBoundary)
{
	m_pBoundaries.push_back(_pBoundary);
}

void JGraph::AddPiece(JPiece* _pPiece)
{
	m_pPieces.push_back(_pPiece);
}

void JGraph::Cleanup()
{
	for_each(m_pPieces.begin(), m_pPieces.end(),
		[](JPiece* _p){delete _p; });
	for_each(m_pBoundaries.begin(), m_pBoundaries.end(),
		[](JBoundary* _p){delete _p; });
	for_each(m_pPoints.begin(), m_pPoints.end(),
		[](JPoint* _p){delete _p; });
	m_pPieces.clear();
	m_pBoundaries.clear();
	m_pPoints.clear();
}

void JGraph::UpdatePointsIds()
{
	int num = (int)m_pPoints.size();
	for (int i = 0; i < num; ++i)
	{
		m_pPoints[i]->SetID(i);
	}
}
